﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace davejk.Minecraft.ServerWrapper.Game
{
    public static class Items
    {

        public static MinecraftItem GetItemByName(string name)
        {
            name = name.Replace(' ', '_').ToLower();
            XmlDocument ItemsDoc = new XmlDocument();
            ItemsDoc.Load(Utils.SetupHelper.AppBaseDirectory + "\\Data\\Items.xml");

            if (ItemsDoc.GetElementById(name) == null)
                return null;

            XmlNode ItemNode = ItemsDoc.GetElementById(name).ParentNode;
            if (ItemNode == null)
                return null;
            else
            {
                if (ItemNode.Attributes["NamePlural"] != null)
                    return new MinecraftItem(ItemNode.Attributes["Name"].Value,
                        ItemNode.Attributes["NamePlural"].Value,
                        ItemNode.Attributes["Id"].Value,
                        Int32.Parse(ItemNode.ParentNode.Attributes["Stack"].Value),
                        (ItemNode.Attributes["OpOnly"].Value == "1"));
                else
                    return new MinecraftItem(ItemNode.Attributes["Name"].Value,
                        ItemNode.Attributes["Name"].Value,
                        ItemNode.Attributes["Id"].Value,
                        Int32.Parse(ItemNode.ParentNode.Attributes["Stack"].Value),
                        (ItemNode.Attributes["OpOnly"].Value == "1"));
            }
        }

    }

    public class MinecraftItem
    {
        public MinecraftItem(string name, string namePlural, string id, int stack = 64, bool opOnly = false)
        {
            _name = name;
            _namePlural = namePlural;
            _id = id;
            _stack = stack;
            _opOnly = opOnly;
        }

        private string _name;
        private string _namePlural;
        private string _id;
        private int _stack;
        private bool _opOnly;

        public string Name { get { return _name; } }
        public string NamePlural { get { return _namePlural; } }
        public string Id { get { return _id; } }
        public int Stack { get { return _stack; } }
        public bool OpOnly { get { return _opOnly; } }

    }
}
